package com.kuyou.publiclibrary.utils;

import static android.content.Context.MODE_PRIVATE;

import android.app.Activity;
import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.MessageDigest;
import java.util.Date;

public class IMEI {



    public  String getDeviceId(Activity content) {
        String deviceId = "";
        try {
            String path = "";
            path = content.getDir("FLX",MODE_PRIVATE).getPath() + "/FLX";
            if (path.equalsIgnoreCase("")) {
                path = content.getDir("FLX", MODE_PRIVATE)
                        .getAbsolutePath();
            }
            path = path + "/FLX/ImeiFile";

            File file = new File(path);
            if (file.exists()) {
                deviceId = loadFileContent(path);
                if (deviceId == "") {
                    deviceId = get_DeviceId();

                 createFile(path, deviceId);
                }
            } else {
                deviceId = get_DeviceId();
                createFile(path, deviceId);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (!deviceId.equalsIgnoreCase("") && deviceId.length() == 32) {


        } else {
            deviceId = "00000000000000000000";
        }
        deviceId = deviceId.substring(0,20);
        Log.e("divice:",deviceId);
        return deviceId;

    }

    public boolean createFile(String path, String content) {

        File srcFile = new File(path);
        File parentFile = srcFile.getParentFile();
        if (!exists(parentFile)) {
            boolean mkdirs = parentFile.mkdirs();

        }

        FileOutputStream fos = null;
        try {
            srcFile.createNewFile();
            fos = new FileOutputStream(srcFile);
            fos.write(content.getBytes());
            fos.close();

            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }
        return false;
    }


    public String loadFileContent(String path) {
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            boolean exists = exists(path);
            if (exists) {
                is = new FileInputStream(path);
            } else {
                return "";
            }
            isr = new InputStreamReader(is);
            br = new BufferedReader(isr);
            String temp = "";
            StringBuilder s = new StringBuilder();
            while ((temp = br.readLine()) != null) {
                if (s.length() > 0) {
                    s.append("\n");
                }
                s.append(temp);
            }
            return s.toString();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != br) {
                    br.close();
                }
            } catch (IOException ignore) {
            }
        }
        return "";
    }




    /**
     * 查看文件是否存在
     *
     * @param path 地址
     * @return true/false
     */
    public boolean exists(String path) {
        return exists(new File(path));
    }

    /**
     * 查看文件是否存在
     *
     * @param file 地址
     * @return true/false
     */
    public boolean exists(File file) {
        return file.exists();
    }



    public String get_DeviceId(){
        Date date = new Date();
        String Time  = String.valueOf(date.getTime());
        Time  = Time+ String.valueOf ((Math.random() * 9 + 1) * 100000000);
        String resultString = digest(Time);
        return resultString;
    }

    public static String digest(String password) {
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            byte[] bytes = digest.digest(password.getBytes());
            StringBuilder sb = new StringBuilder();
            for (byte b : bytes) {
                int c = b & 0xff; //负数转换成正数
                String result = Integer.toHexString(c); //把十进制的数转换成十六进制的书
                if(result.length()<2){
                    sb.append(0); //让十六进制全部都是两位数
                }
                sb.append(result);
            }
            return sb.toString(); //返回加密后的密文
        } catch (Exception ex) {
            ex.printStackTrace();
            return "";
        }
    }
}
